package com.hqj.demo.controller;

import com.hqj.demo.entity.User;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;

@Controller
@RequestMapping("/tc02")
public class Test02Controller {

    @InitBinder
    public void initBinder(WebDataBinder wdb){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        CustomDateEditor sde = new CustomDateEditor(sdf,true);
        wdb.registerCustomEditor(Date.class,sde);
    }

    @RequestMapping("/t01")
    public String t01(HttpServletRequest request){
        String url = request.getParameter("url");
        String name = request.getParameter("name");
        return "/"+url;
    }

    @RequestMapping("/t02")
    @ResponseBody
    public User getUser(User u){
        return u;
    }

    @RequestMapping("/t03/{url}")
    public String t03(@PathVariable(value = "url") String url){
        return "/"+url;
    }

    @RequestMapping("/t04")
    public String t04(@RequestParam(value = "name") String name,
                      @RequestParam(value="age") int age){
        return "/index";
    }

}
